package com.znxs.bs_c.service.impl;

import com.znxs.bs_c.common.Result;
import com.znxs.bs_c.entity.Role;
import com.znxs.bs_c.mapper.RoleMapper;
import com.znxs.bs_c.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 左拿
 * @since 2023-08-21
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Result getMenuIdByRoleId(Integer roleId) {
        List<Integer> menuIds = roleMapper.getMenuIdByRoleId(roleId);
        return Result.success(menuIds);
    }

    @Override
    public Result addMenuIdAndRoleId(Integer roleId, List<Integer> menuIds) {
        boolean flag = false;
        // 先删除所有相关的菜单
        roleMapper.deleteMenuIdAndRoleId(roleId);
        // 再添加选中的菜单
        for (Integer menuId :menuIds) {
            if(roleMapper.addMenuIdAndRoleId(roleId, menuId))flag=true;
        }
        return flag?Result.success():Result.error(500,"添加错误");
    }
}
